//
//  RYCListModelIndexPagingPolicy.m
//  Cashier
//
//  Created by hemingway payegis on 13-2-1.
//  Copyright (c) 2013年 payegis. All rights reserved.
//

#import "RYCListModelIndexPagingPolicy.h"

@interface RYCListModelIndexPagingPolicy ()
{
    NSInteger _requestIndex;
}
@end

@implementation RYCListModelIndexPagingPolicy

- (id)init
{
    self = [super init];
    if (self)
    {
        _pageSize = 20;
    }
    return self;
}

- (NSDictionary*)pagingParamsForSendRequest:(RYCHTTPListModel*)listModel
{
    NSMutableDictionary* dict = [NSMutableDictionary dictionary];
    
    if (RYCHTTPRequestModelOperation_Normal == listModel.operation
        || RYCHTTPRequestModelOperation_Refresh == listModel.operation)
    {
        _requestIndex = 1;
    }
    else if (RYCHTTPRequestModelOperation_Forward ==  listModel.operation)
    {
        _requestIndex = _currentPageIndex + 1;
    }
    else if (RYCHTTPRequestModelOperation_Backward == listModel.operation)
    {
        if (_currentPageIndex > 1)
        {
            _requestIndex = _currentPageIndex - 1;
        }
        else
        {
            _requestIndex = 1;
        }
    }
   
    [dict setObject:[NSNumber numberWithInteger:_requestIndex] forKey:@"pageIndex"];
    [dict setObject:[NSNumber numberWithInteger:_pageSize] forKey:@"pageSize"];
    return dict;
}

- (void)didModelFinished:(RYCHTTPListModel*)listModel
{
    _currentPageIndex = _requestIndex;
}

- (void)reset:(RYCHTTPListModel*)listMode
{
    _currentPageIndex = 0;
    _requestIndex = 0;
}

- (BOOL)hasNextPage:(RYCHTTPListModel*)listMode
{
    if ([listMode.lastResponseList count] < _pageSize)
    {
        return NO;
    }
    return YES;
}
@end
